<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <!-- 避免IE使用兼容模式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <!--<link rel="stylesheet" type="text/css" href="/easyui/themes/metro/easyui.css"/>-->
    <!-- TopJUI框架样式 -->
    <link type="text/css" href="/topjui/css/topjui.core.min.css" rel="stylesheet">
    <link type="text/css" href="/topjui/themes/default/topjui.bluelight.css" rel="stylesheet" id="dynamicTheme"/>
    <!-- FontAwesome字体图标 -->
    <link type="text/css" href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>
    <!-- layui框架样式 -->
    <link type="text/css" href="/static/plugins/layui/css/layui.css" rel="stylesheet"/>
    <link type="text/css" href="/topjui/css/myui.css" rel="stylesheet"/>
    <!--<script type="text/javascript" src="/static/public/js/topjui.config.js"></script>-->
    <!--<script type="text/javascript" src="/common/jquery/jquery-1.6.2.min.js"></script>-->
    <!--<script type="text/javascript" src="/topjui/js/topjui.core.js"></script>-->
    <!-- layui框架js -->
    <script type="text/javascript" src="/static/plugins/layui/layui.js" charset="utf-8"></script>
    <script src="/easyui/jquery.min.js"></script>
    <script src="/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="/topjui/js/sysutils.js"></script>
</head>

<body>

<table id="rowgrid" class="topjui-datagrid"></table>

<script type="text/javascript" charset="utf-8" th:inline="none">
    var listgrid;
    $(function(){
        listgrid = $('#rowgrid').datagrid({
            url:"/content/list",
            method:'get',
            fit : true,
            fitColumns : true,
            border : false,
            pagination : true,
            idField : 'id',
            pageSize : 20,
            pageList : [ 20, 50, 100 ],
            sortName : 'id',
            sortOrder : 'desc',
            checkOnSelect : true,
            selectOnCheck : true,
            nowrap : false,
            columns:[[
                {
                    field:'id',
                    title:'编号',
                    width:50,
                    checkbox : true
                }, {
                    field:'title',
                    title:'栏目名称',
                    sortable:true,
                    width:50
                }, {
                    field:'categoryid',
                    title:'所属栏目',
                    hidden:true
                }, {
                    field:'categoryname',
                    title:'所属栏目',
                    width:50
                }, {
                    field:'tagid',
                    title:'标签',
                    hidden:true
                }, {
                    field:'tagname',
                    title:'标签',
                    width:50
                },{
                    field:'author',
                    title:'作者',
                    width:40
                }, {
                    field:'source',
                    title:'来源',
                    sortable:true,
                    width:40
                }, {
                    field:'top',
                    title:'推荐',
                    width:30,
                    formatter : function(value, row, index) {
                        if(value == '0'){
                            return '<span style="color:green">否</span>';
                        }else{
                            return '<span style="color:blue">是</span>';
                        }
                    }
                }, {
                    field:'hot',
                    title:'热门',
                    width:30,
                    formatter : function(value, row, index) {
                        if(value == '1'){
                            return '<span style="color:green">是</span>';
                        }else{
                            return '<span style="color:dimgray">否</span>';
                        }
                    }
                }, {
                    field:'pic',
                    title:'图片新闻',
                    width:30,
                    formatter : function(value, row, index) {
                        if(value == '1'){
                            return '<span style="color:green">是</span>';
                        }else{
                            return '<span style="color:dimgray">否</span>';
                        }
                    }
                },  {
                    field:'status',
                    title:'状态',
                    width:30,
                    align:'center',
                    formatter : function(value, row, index) {
                        if(value == '1'){
                            return '<span style="color:green">正常</span>';
                        }else{
                            return '<span style="color:red">禁用</span>';
                        }
                    }
                }, {
                    field : 'action',
                    title : '操作',
                    width : 50,
                    align:'center',
                    formatter : function(value, row, index) {
                        return formatString('<img onclick="list_editFun(\'{0}\');" style="cursor:pointer" src="{1}"/>&nbsp;&nbsp;&nbsp;<img style="cursor:pointer" onclick="list_deleteFun(\'{2}\');" src="{3}"/>', row.id, '/static/admin/images/pencil.png', row.id, '/static/admin/images/edit_remove.png');
                    }
                }
            ]],
            toolbar:[{
                text:'新建',
                iconCls:'fa fa-plus',
                btnCls: 'topjui-btn-normal',
                handler:function(){
                    list_addFun();
                }
            },'-',{
                text:'删除',
                iconCls:'fa fa-trash',
                btnCls: 'topjui-btn-normal',
                handler:function(){
                    list_removeFun();
                }
            },'-', {
                text:'刷新',
                iconCls : 'fa fa-refresh',
                btnCls: 'topjui-btn-green',
                handler : function() {
                    listgrid.datagrid('reload');
                }
            }],
            onDblClickRow :function(rowIndex,rowData){
                list_editFun(rowData.id);
            }
        });
    });

    function list_addFun(){
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        $('<div/>').dialog({
            href : "/content/form",
            width : '960',
            height : '760',
            modal : true,
            title : '添加信息',
            buttons : [{
                text : '保存',
                iconCls : 'fa fa-save',
                btnCls: 'topjui-btn-normal',
                handler : function() {
                    var d = $(this).closest('.window-body');
                    $('#customerform').form('submit', {
                        url : "/content/save",
                        success : function(result) {
                            try {
                                var r = $.parseJSON(result);
                                if (r.statusCode=='200') {
                                    listgrid.datagrid('load');
                                    d.dialog('destroy');
                                }
                                $.messager.show({
                                    title : r.title,
                                    msg : r.message
                                });
                            } catch (e) {
                                $.messager.alert('提示', result);
                            }
                        }
                    });
                }
            }],
            onClose : function() {
                $(this).dialog('destroy');
            }
        });
    }

    function list_contentFun(id){
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        $('<div/>').dialog({
            href : "/content/content?id="+id,
            width : 800,
            height : 600,
            modal : true,
            title : '编辑内容',
            buttons : [ {
                text : '保存',
                iconCls : 'fa fa-save',
                btnCls: 'topjui-btn-normal',
                handler : function() {
                    var d = $(this).closest('.window-body');
                    $('#customerform').form('submit', {
                        url : "/content/savetext",
                        success : function(result) {
                            try {
                                var r = $.parseJSON(result);
                                if (r.statusCode=='200') {
                                    listgrid.datagrid('load');
                                    d.dialog('destroy');
                                }
                                $.messager.show({
                                    title : r.title,
                                    msg : r.message
                                });
                            } catch (e) {
                                $.messager.alert('提示', result);
                            }
                        }
                    });
                }
            } ],
            onClose : function() {
                $(this).dialog('destroy');
            }
        });
    }

    function list_editFun(id) {
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        $('<div/>').dialog({
            href : "/content/edit?id="+id,
            width : '960',
            height : '760',
            modal : true,
            title : '编辑信息',
            buttons : [ {
                text : '保存',
                iconCls : 'fa fa-save',
                btnCls: 'topjui-btn-normal',
                handler : function() {
                    var d = $(this).closest('.window-body');
                    $('#customerform').form('submit', {
                        url : "/content/update",
                        success : function(result) {
                            try {
                                var r = $.parseJSON(result);
                                if (r.statusCode=='200') {
                                    listgrid.datagrid('load');
                                    d.dialog('destroy');
                                }
                                $.messager.show({
                                    title : r.title,
                                    msg : r.message
                                });
                            } catch (e) {
                                $.messager.alert('提示', result);
                            }
                        }
                    });
                }
            } ],
            onClose : function() {
                $(this).dialog('destroy');
            },
            onLoad : function() {
                var index = listgrid.datagrid('getRowIndex', id);
                var rows = listgrid.datagrid('getRows');
                var o = rows[index];
                $('#customerform').form('load', o);
            }
        });
    }

    function list_removeFun() {
        var rows = listgrid.datagrid('getChecked');
        var ids = [];
        if (rows.length > 0) {
            console.info(rows);
            $.messager.confirm('确认', '您是否要删除当前选中的项目？', function(r) {
                if (r) {
                    for ( var i = 0; i < rows.length; i++) {
                        ids.push(rows[i].id);
                    }
                    $.ajax({
                        url : '/content/remove',
                        data : {
                            ids : ids.join(',')
                        },
                        dataType : 'json',
                        success : function(r) {
                            if (r.statusCode=='200') {
                                listgrid.datagrid('load');
                                listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                            }
                            $.messager.show({
                                title : r.title,
                                msg : r.message
                            });
                        }
                    });
                }
            });
        } else {
            $.messager.show({
                title : '提示',
                msg : '请勾选要删除的记录！'
            });
        }
    }

    function list_deleteFun(id) {
        listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        $.messager.confirm('确认', '您是否要删除当前选中的项目？', function (r) {
            if (r) {
                $.ajax({
                    url: '/content/delete',
                    data: {
                        ids: id
                    },
                    dataType: 'json',
                    success: function (r) {
                        if (r.statusCode=='200') {
                            listgrid.datagrid('load');
                            listgrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                        }
                        $.messager.show({
                            title: '提示',
                            msg: r.message
                        });
                    }
                });
            }
        });
    }
</script>

</body>
</html>